PHP: parse $_FILES[] data in multidimesional array

Posted by superUntitled on Stack Overflow See other posts from Stack Overflow or by superUntitled
Published on 2010-12-23T06:15:45Z Indexed on 2010/12/29 3:54 UTC
Read the original article Hit count: 174

Filed under:
|

Example form here: http://jsfiddle.net/superuntitled/uaTtx/1/

I have a form that allows for dynamic duplication of the form fields. The form allows for file uploads and text input, so the data is sent in both $_POST and $_FILES arrays.

The the initial set of inputs look like this:

<input type="text" name="question[1][text]"  />
<input  type="file" name="question[1][file]"  /> 

<input type="text" class="a" name="answer[1][text][]"  /> 
<input  type="file" name="answer[1][file][]"  /> 

When duplicated the fields are incremented, they look like this:

<input type="text" name="question[2][text]"  />
<input  type="file" name="question[2][file]"  /> 

<input type="text" class="a" name="answer[2][text][]"  /> 
<input  type="file" name="answer[2][file][]"  /> 

To complicate matters, the "answer" form fields can also be duplicated (thus the [] at the end of the 'answer' name array.

How can I parse the posted $_FILES array? I have tried something like this:

foreach ($_FILES['question'] as $p_num)
{ 
  echo  $p_num['file']['name'];

    foreach ($_FILES['answer'] as $a_num)
    { 
      echo  $a_num['file']['name'];
    }

}

but I get an "Undefined index: file... " error. How can I parse out the posted values.

© Stack Overflow or respective owner

PHP: parse $_FILES[] data in multidimesional array

Posted by superUntitled on Stack Overflow See other posts from Stack Overflow or by superUntitled
Published on 2010-12-29T00:50:49Z Indexed on 2010/12/29 0:53 UTC
Read the original article Hit count: 175

Filed under:
|

I having been looking around for an answer to this and have not found an answer anywhere, I am hoping someone has done this before! I have a form that allows for dynamic duplication of the form fields. The form allows for file uploads and text input, so the data is sent in both $_POST and $_FILES arrays.

The the initial set of inputs look like this:

<input type="text" name="primary[1][text]"  />
<input  type="file" name="primary[1][file]"  /> 

<input type="text" class="a" name="secondary[1][text][]"  /> 
<input  type="file" name="secondary[1][file][]"  /> 

When duplicated the fields are incremented, they look like this:

<input type="text" name="primary[2][text]"  />
<input  type="file" name="primary[2][file]"  /> 

<input type="text" class="a" name="secondary[2][text][]"  /> 
<input  type="file" name="secondary[2][file][]"  /> 

To complicate matters, the "secondary" form fields can also be duplicated (thus the [] at the end of the secondary name array.

How can I parse the posted $_FILES array? I have tried something like this:

foreach ($_FILES['question'] as $f_num)
{ 
  echo  $f['file']['name'];
}

but I get an "Undefined index: file... " error.

© Stack Overflow or respective owner

Related posts about php

Related posts about file-upload